<?php

declare(strict_types=1);

namespace app\controller;

use app\service\CodeService;
use Dsxwk\Framework\Exception\CodeException;
use support\Request;
use support\Response;
use thiagoalessio\TesseractOCR\TesseractOcrException;

class CodeController
{
    /**
     * 获取验证码
     *
     * @param CodeService $service
     *
     * @return Response
     */
    public function get(CodeService $service): Response
    {
        return apiResponse($service->get());
    }

    /**
     * 验证码校验
     *
     * @param Request     $request
     * @param CodeService $service
     *
     * @return Response
     */
    public function verify(Request $request, CodeService $service): Response
    {
        return apiResponse($service->verify($request->input('key'), $request->input('code')));
    }

    /**
     * OCR识别
     *
     * @param Request     $request
     * @param CodeService $service
     *
     * @return Response
     * @throws TesseractOcrException
     */
    public function ocr(Request $request, CodeService $service): Response
    {
        $image = public_path('test.png');

        return apiResponse($service->ocr($image));
    }
}
